/**
 * @file   main.cpp
 * @author Zhao jingting <jtzhao@Zhao jingting.
 * @date   The Oct 20 13:24:23 2021
 *
 * @brief  A program to test.
 *
 *
 */



#include<iostream>
#include"Vector.h"
#include<ctime>
#include<cstdlib>
#define N 10000
using namespace std;

/**
 * main function.
 *
 * @param argc number of shell parameters.
 * @param argv strings for all parameters.
 *
 * @return system signals.
 */


int main(int argc, char* argv[])
{
  //随机数种子
  srand(time(0));
  //压入Vector的元素，固定为1
  int element = 1;

  //声明stacwaste数组用于储存不同的SPARE_CAPACITY对应的内存浪费之和，并初始化为0
  int stackwaste[20];
  for (int n = 0; n < 20; n++)
    {
      stackwaste[n] = 0;
    }

  //声明开始结束时间
  clock_t start, end;

  //声明stac数组用于储存不同的SPARE_CAPACITY对应的时间耗费和，并初始化为0
  clock_t stac[20];
  for (int n = 0; n < 20; n++)
    {
      stac[n] = 0;
    }

  //通过循环变量j控制SPARE_CAPACITY的不同取值
  for (int j = 1; j <= 20; j++)
    {
      //开始计时
      start = clock();
      //通过循环变量n控制运行次数增大时间
      for (int n = 0; n < 100; n++)
	{
	  //模拟1～N均匀分布
	  for (int i = 1; i <= 10; i++)
	    {
	      //初始化Vector
	      int size = N / 10 *i;
	      int initsize = 0;
	      Vector<int> V(initsize);
	      //更改SPARE_CAPACITY
	      V.SPARE_CAPACITY = N / 20*j;
	      V.theCapacity = V.SPARE_CAPACITY + V.theSize;
	      V.reserve(V.theCapacity);
	      for (int i = 0; i < (size - initsize); i++)
		{
		  V.push_back(element);
		}
	      //计算内存浪费
	      stackwaste[j - 1] += V.capacity() - V.size();
	    }
	  //计时结束
	  end = clock();
	  //计算时间耗费
	  stac[j - 1] += end - start;
	}
    }
  clock_t stactotall = 0;
  int stackwastetotall = 0;

  //计算总是见域内村耗费便于后续计算百分值
  for (int n = 0; n < 20; n++)
    {
      stackwastetotall += stackwaste[n];
    }
  for(int n=0; n<20; n++)
  {
    stactotall += stac[n];
  }

  //输出时间耗费部分
  cout << "time comparasion percentage: " << endl;
  for (int n = 0; n < 20; n++)
    {
      cout <<n*5<<"%N:"<<'\t'<< double(stac[n]) / 1000<<"s"<<'\t'<<double(stac[n])/double(stactotall)*100 <<"%" << endl;
    }
  cout << endl << endl;

  //输出内存耗费部分
  cout << "the stackwaste percentage: " << endl;
  for (int n = 0; n < 20; n++)
    {
      cout << n * 5 << "%N:" << '\t' <<stackwaste[n]<<'\t'<<double(stackwaste[n])/double(stackwastetotall)*100<< "%" << endl;
    }
  cout << endl << endl;

  //输出时间空间融合值
  cout << "time and space percentage merge: " << endl;for (int n = 0; n < 20; n++)
							{
							  cout << n * 5 << "%N:" << '\t' << double(stac[n]) / double(stactotall) * 100 * double(stackwaste[n]) / double(stackwastetotall) * 100 << endl;
							}
}
